<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/5
 * Time: 11:49
 */

namespace yiike\order\components;


class Express
{
     static $states = ['在途', '揽件', '疑难', '签收', '退签', '派件', '退回'];


    public static function get($typeCom, $typeNu) {
        $cache_key = md5($typeCom.$typeNu);
        $cache = cache_load($cache_key);
        if ($cache) {
            return $cache;
        }
        $url = "https://www.kuaidi100.com/query?type={$typeCom}&postid={$typeNu}&temp=0.5861342323675607";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'. \Yii::$app->request->userIP, 'CLIENT-IP:'.\Yii::$app->request->userIP));
        curl_setopt($curl, CURLOPT_REFERER, 'http://www.baidu.com/');//模拟来路
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $get_content = curl_exec($curl);
        curl_close($curl);

        if ($get_content) {
            $arr = json_decode($get_content, true);
            if (isset($arr) && $arr['status'] == 200) {
                $model = pdo_get('ttyc_shop_express', ['code' => $typeCom]);
                $data = [
                    'com' => $model ? $model['name'] : $typeCom,
                    'tel' => $model ? $model['tel'] : '',
                    'nu' => $typeNu,
                    'state' => $arr['state'],
                    'state_text' => self::$states[$arr['state']],
                    'data' =>  $arr['data']
                ];
                cache_write($cache_key, $data, 3600);
                return $data;
            }
        }

        return false;
    }
}
